package edu.hqu.rent_system.controller;


import edu.hqu.rent_system.common.Result;
import edu.hqu.rent_system.pojo.LandLord;
import edu.hqu.rent_system.pojo.Tenant;
import com.alibaba.fastjson.JSON;
import edu.hqu.rent_system.service.LandLordService;
import edu.hqu.rent_system.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import edu.hqu.rent_system.config.passwordCoding;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private TenantService tenantService;
    @Autowired
    private LandLordService landLordService;
    //租客登录
    @RequestMapping("/loginTenant")
    public Result queryTenantInform(@RequestParam int id, @RequestParam String password){
        try{
            if(id != 0){
                Tenant tenant = tenantService.queryTenantInform(id);
                String passwordCoding = edu.hqu.rent_system.config.passwordCoding.getHash2(password,"MD5");
                System.out.println(tenant);
                if(tenant.getPassword().equals(passwordCoding)){
                    return Result.success(1);
                }else{
                    return Result.fail();
                }
            }else{
                return Result.fail();
            }
        }catch (Exception e){
            return Result.fail();
        }
    }
    //租客注册
    @RequestMapping("/registerTenant")
    public Result addTenant(@RequestBody String tenant){

        try {

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

            Tenant tenant1 = JSON.parseObject(tenant, Tenant.class);
            tenant1.setBirthday(df.format(Long.parseLong(tenant1.getBirthday())));

            tenant1.setPassword(passwordCoding.getHash2(tenant1.getPassword(), "MD5"));
            String phone = tenant1.getPhone();
            System.out.println(phone);
            List<Tenant> tenantList = tenantService.queryAllTenant();

            for (Tenant tenantTemp : tenantList) {
                if (tenantTemp.getPhone().equals(phone)) {

                    return Result.fail();
                }
            }

            int res = tenantService.addTenant(tenant1);
            if (res == 1) {
                Tenant t = tenantService.queryTenantByPhone(tenant1.getPhone());
                int id = t.getTId();
                System.out.println(id);

                return Result.success(id);
            } else {

                return Result.fail();
            }
        }catch (Exception e){
            return Result.fail();
        }

    }
    //租客注销
    @RequestMapping("/deleteTenant")
    public Result deleteTenant(@RequestParam int id){
        try{
            if(id != 0){
                int res = tenantService.deleteTenant(id);
                if(res == 1){
                    return Result.success(res);
                }else{
                    return Result.fail();
                }
            }else{
                return Result.fail();
            }
        }catch(Exception e){
            return Result.fail();
        }
    }
    //房东登录
    @RequestMapping("/loginLandLord")
    public Result loginLandLord(@RequestParam int id, @RequestParam String password){
        try{
            if(id != 0){
                LandLord landLord = landLordService.loginLandLord(id);
                String passwordCoding = edu.hqu.rent_system.config.passwordCoding.getHash2(password,"MD5");
                if(landLord.getPassword().equals(passwordCoding)){
                    return Result.success(1);
                }else{
                    return Result.fail();
                }
            }else{
                return Result.fail();
            }
        }catch(Exception e){
            return Result.fail();
        }
    }
    //房东注册
    @RequestMapping("/registerLandLord")
    public Result registerLandLord(@RequestBody String landLord){

        try{

            LandLord landLord1 = JSON.parseObject(landLord, LandLord.class);

            landLord1.setPassword(passwordCoding.getHash2(landLord1.getPassword(), "MD5"));
            List<LandLord> landLordList = landLordService.queryAllTLandlord();
            System.out.println(landLord1);

            for (LandLord landlord : landLordList
            ) {
                if (landlord.getPhone().equals(landLord1.getPhone())) {
                    return Result.fail();
                }
            }

            int res = landLordService.registerLandLord(landLord1);
            if (res == 1) {
                LandLord landLord2 = landLordService.queryLandlordByPhone(landLord1.getPhone());
                System.out.println(landLord2);

                int id = landLord2.getLId();
                return Result.success(id);
            } else {
                return Result.fail();
            }
        }catch (Exception e){
            return Result.fail();
        }


    }

    //房东注销
    @RequestMapping("/deleteLandLord")
    public Result deleteLandLord(@RequestParam int id){
        try{
            if(id != 0){
                int res =landLordService.deleteLandLord(id);
                if(res == 0){
                    return Result.success(res);
                }else{
                    return Result.fail();
                }
            }else{
                return Result.fail();
            }

        }catch(Exception e){
            return Result.fail();
        }
    }

}
